package com.justech.mes.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.justech.mes.service.RabbitMqSendService;
import com.justech.mes.v2.model.vo.EquipmentVo;
import com.justech.mes.v2.service.MesEquipCollectV2Service;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class RabbitSendServiceImpl implements RabbitMqSendService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private MesEquipCollectV2Service mesEquipCollectV2Service;

    @Override
    public void send(String msg) {
        JSONObject jsonObject = null;
        jsonObject = JSONObject.parseObject(msg);
        String key = jsonObject.get("key").toString();
        if (!StringUtils.isEmpty(key)) {
            String data = jsonObject.get("data").toString();
            EquipmentVo equipmentVo = JSONObject.parseObject(data, EquipmentVo.class);
            mesEquipCollectV2Service.add(equipmentVo);
            amqpTemplate.convertAndSend("", key, "@pass");
        }
    }
}
